package face.pay.web.enterprise.controller.store;

import face.pay.common.enterprise.service.IStoreInfoBaseService;
import face.pay.entity.StoreInfo;
import face.pay.entity.UserInfo;
import face.pay.security.annotation.CurrentUser;
import face.pay.security.exception.BaseException;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: zkj
 * @Date: 2019/7/15 09:37
 * @Description:商户门店管理
 */
@Controller
@RequestMapping("/enterprise_store")
public class StoreInfoController {


    @Autowired
    private IStoreInfoBaseService iStoreInfoBaseService;


    @RequestMapping("/list")
    public String manager() {
        return "enterprise/store_list";
    }

    @RequestMapping("/store_detailed_list")
    public String storeDetailedList() {
        return "enterprise/store_detailed_list";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ResultMessage editUser(@CurrentUser UserInfo loginUser, StoreInfo storeInfo) throws BaseException {

        return iStoreInfoBaseService.modifyInfo(storeInfo, loginUser);

    }

    @RequestMapping("/queryData")
    @ResponseBody
    public PageData data(PageParam pageParam, StoreInfo storeInfo, @CurrentUser UserInfo userInfo) throws BaseException {

        storeInfo.setUserId(userInfo.getId());
        return this.iStoreInfoBaseService.queryPage(storeInfo, pageParam);

    }

    @RequestMapping("getStores")
    @ResponseBody
    public ResultMessage getStores(StoreInfo storeInfo, @CurrentUser UserInfo userInfo) throws BaseException {

        storeInfo.setUserId(userInfo.getId());
        return this.iStoreInfoBaseService.getStores(storeInfo);

    }

    /**
     * 保存门店详细信息
     *
     * @param loginUser 当前登录人信息
     * @param storeInfo 门店详细信息
     */
    @RequestMapping("addStoreDetailedInfo")
    @ResponseBody
    public ResultMessage addStoreDetailedInfo(StoreInfo storeInfo, @CurrentUser UserInfo loginUser) throws BaseException {
        return this.iStoreInfoBaseService.addStoreDetailedInfo(loginUser, storeInfo);
    }

    /**
     * 升级普通门店成为优惠券门店
     */
    @RequestMapping("upgradeStore")
    @ResponseBody
    public ResultMessage upgradeStore(Integer id,@CurrentUser UserInfo loginUser) throws BaseException {
        return this.iStoreInfoBaseService.upgradeStore(id,loginUser);
    }

    /**
     * 上传图片
     */
    @RequestMapping("/upload")
    @ResponseBody
    public ResultMessage uplpad(@CurrentUser UserInfo loginUser, MultipartFile file, Integer storeId) throws IOException {
        String oriName = file.getOriginalFilename();
        byte[] bytes = file.getBytes();
        return iStoreInfoBaseService.fielUpload(bytes,oriName,loginUser,storeId);
    }

    /**
     * 清空商家门店图片
     * @param storeId
     * @return
     * @throws IOException
     */
    @RequestMapping("/deleteStoreMaterialInfo")
    @ResponseBody
    public ResultMessage deleteStoreMaterialInfo(@CurrentUser UserInfo loginUser,Integer storeId) throws IOException {
        return iStoreInfoBaseService.deleteStoreMaterialInfo(loginUser,storeId);
    }

}
